import mysql = require("mysql")
import http = require("http")
import { relative, resolve } from "path/posix";
import { rejects } from "assert";

export class DBServer {

    public m_pMysql: mysql.Connection;

    public m_pDB = {
        host: 'localhost',
        user: 'root',
        password: 'root',
        database: 'user',
        charset: 'UTF8_GENERAL_CI',
    }

    public constructor() {
        this.m_pMysql = mysql.createConnection(this.m_pDB);
        this.m_pMysql.connect((err) => {
            if (err) {
                return console.error('error connecting: ' + err.stack);
            }
            console.log('connected as id ' + this.m_pMysql.threadId);
        });
    }

    //查询
    public SeleAllData(): Promise<unknown> {
        let sql = "SELECT * FROM userinfo";
        const promise = new Promise((resolve, rejects) => {
            this.m_pMysql.query(sql, (err, results, fields) => {
                if (err)
                    throw err;

                resolve(results);
            })
        })
        return promise;
    }

    //登录
    public SeleData(uname: string, pwd: string, res: any) {

        let sql = "SELECT * FROM userinfo WHERE username = '" + uname + "' AND password = " + pwd;
        this.m_pMysql.query(sql, (err, results, fields) => {
            if (err) {
                return res.send(err)
            }
            res.send(results.length == 0 ? "用户名或密码错误！" : "登录成功！ 欢迎您" + uname)
        })
    }

    //添加数据
    public async AddData(name: string, sex: number, age: number, uname: string, pwd: string, res) {


        let seleSql = "select username from userinfo where username = '" + uname + "'";
        let inseSql = "INSERT INTO userinfo (name, sex, age, username, password) VALUES ('" + name + "', '" + sex + "', " + age + ", '" + uname + "', " + pwd + ")";
        await this.m_pMysql.query(seleSql, (err, results) => {
            if (err) {
                return res.send(JSON.stringify(err));
            }
            if (results.length >= 1) {
                return res.send("失败，用户名重复");
            }
        })

        this.m_pMysql.query(inseSql, (err, results) => {

            if (err) {
                return res.send(err)
            }
            res.send(results.affectedRows ? '恭喜您：' + name + '，注册成功！' : '注册失败！');
        })

        // let promise = new Promise(function (resolve, reject) {  

        // })
        // promise.then(

        // ).then(

        // )

        // Promise.reject()

    }


    //修改数据
    public UpdateData(uname: string, age: number, res: any) {

        let sql = "UPDATE userinfo SET age = " + age + " WHERE username = '" + uname + "'";

        this.m_pMysql.query(sql, (err, results) => {

            if (err) {
                return res.send(err)
            }
            res.send(results.affectedRows ? "年龄已修改为" + age : '修改失败！');
        })
    }

    //删除数据
    public RemoveData(uname: string, res: any) {

        let sql = "DELETE FROM userinfo WHERE username = '" + uname + "'";

        this.m_pMysql.query(sql, (err, results) => {

            if (err) {
                return JSON.stringify(err)
            }
            console.log(results);
            res.send(results.affectedRows ? '账号信息注销成功！' : '暂无此用户')
        })
    }




}